<!-- 企业编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="640"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="付款时间" prop="pay_time">
        <el-date-picker
          v-model="form.pay_time"
          class="filter-item"
          type="datetime"
          placeholder="付款时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 230px"
          :clearable="false"
        />
      </el-form-item>
      <el-form-item label="付款金额" prop="amount">
        <el-input v-model="form.amount" style="width: 150px">
          <template #append>元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="图片" prop="imagesList">
        <image-upload :limit="100" v-model="form.imagesList" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, watch, reactive } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { insurancePay } from '@/api/company';

  import ImageUpload from '@/components/ImageUpload/index.vue';

  const emit = defineEmits(['done', 'update:modelValue']);

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object
  });

  /** 表单实例 */
  const formRef = ref(null);

  const loading = ref(false);

  /** 表单数据 */
  const [form] = useFormData({
    id: 0,
    amount: '',
    imagesList: '',
    pay_time: ''
  });
  /** 表单验证规则 */
  const rules = reactive({
    imagesList: [
      {
        required: true,
        message: '请上传付款图片',
        type: 'string',
        trigger: 'blur'
      }
    ],
    amount: [
      {
        required: true,
        message: '请输入付款金额',
        type: 'string',
        trigger: 'blur'
      }
    ],
    pay_time: [
      {
        required: true,
        message: '请选择付款时间',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /** 保存编辑 */
  const save = () => {
    console.log(form);
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      var requestData = [
        {
          id: form.id,
          cash: 1,
          price: props.data.price,
          tax_price: props.data.tax_price,
          rebate_amount: props.data.rebate_amount,
          plate_number: props.data.plate_number
        }
      ];
      requestData = JSON.stringify(requestData);
      // 这里保险付款，是现结的，去买保险，和后结的公用一个接口，后结有批量付款，所以这里也要做成list的形式
      insurancePay({
        amount: form.amount,
        type: '全款',
        list: requestData,
        images: form.imagesList,
        pay_time: form.pay_time,
        trade_id: props.data.trade_id
      })
        .then(() => {
          loading.value = false;
          EleMessage.success('操作成功');
          updateModelValue(false);
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error(e.message);
        });
    });
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  const title = ref('');
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        form.id = props.data.id;
        form.imagesList = '';
        form.pay_time = '';
        form.amount = props.data.totalPrice;
        title.value =
          '付' +
          props.data.plate_number +
          '的' +
          props.data.totalPrice +
          '元保险费给' +
          props.data.insurance;
      } else {
        console.log('无');
      }
    }
  );
</script>
